<% add_decidim_page_title(t("decidim.pages.index.title")) %>
<%
edit_link(
  decidim_admin.static_pages_path,
  :read,
  :static_page
)
%>

<%= render layout:"layouts/decidim/shared/layout_center", locals: { columns: 8 } do %>
  <div class="text-center py-10">
    <h1 class="title-decorator inline-block text-left mb-12">
      <%= t "decidim.pages.index.title" %>
    </h1>
    <p class="text-lg text-gray-2">
      <%= t("decidim.pages.index.subheading", name: current_organization_name) %>
    </p>
  </div>

  <div class="space-y-16">
    <% if @topics.any? %>
      <div class="space-y-6">
        <h2 class="h2 decorator"><%= t "decidim.pages.index.topics" %></h2>

        <div class="space-y-6" data-controller="accordion" id="topics-accordion">
          <% @topics.each_with_index do |topic, ix| %>
            <% if topic.pages.any? %>
              <div class="page__accordion">
                <div class="page__accordion-trigger">
                  <div>
                    <h3 id="accordion-title-<%= ix %>" class="page__accordion-trigger-title">
                      <%= link_to translated_attribute(topic.title), page_path(topic.pages.first, locale: current_locale) %>
                    </h3>
                    <p class="page__accordion-trigger-snippet"><%= translated_attribute topic.description %></p>
                  </div>
                  <button id="accordion-trigger-<%= ix %>" data-controls="accordion-panel-<%= ix %>" aria-labelledby="accordion-title-<%= ix %>">
                    <%= icon "arrow-down-s-line", class: "page__accordion-trigger-arrow" %>
                    <%= icon "arrow-up-s-line", class: "page__accordion-trigger-arrow" %>
                  </button>
                </div>

                <div id="accordion-panel-<%= ix %>" class="page__accordion-panel" aria-hidden="true">
                  <ul role="menu">
                    <% topic.pages.each do |page| %>
                      <li role="menuitem"><%= link_to translated_attribute(page.title), page_path(page, locale: current_locale), class: "text-secondary" %></li>
                    <% end %>
                  </ul>
                </div>
              </div>
            <% end %>
          <% end %>
        </div>
      </div>
    <% end %>

    <% if @orphan_pages.any? %>
      <div class="space-y-6">
        <h2 class="h2 decorator"><%= t "decidim.pages.index.standalone_pages" %></h2>
        <div class="page__grid">
          <% @orphan_pages.each do |page| %>
            <div class="page__accordion">
              <h3 class="page__accordion-trigger-title">
                <%= link_to translated_attribute(page.title), page_path(page, locale: current_locale) %>
              </h3>
              <p class="page__accordion-trigger-snippet"><%== strip_tags html_truncate(translated_attribute(page.content), length: 140, separator: "...") %></p>
            </div>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>

<% end %>
